<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="场地名称" prop="locationName">
        <el-input
          v-model="queryParams.locationName"
          placeholder="请输入场地名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="建筑物ID" prop="buildingId">
        <el-input-number
          v-model="queryParams.buildingId"
          placeholder="请输入建筑物ID"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input-number
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomNumber">
        <el-input
          v-model="queryParams.roomNumber"
          placeholder="请输入房间号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="场地类型" prop="locationType">
        <el-select
          v-model="queryParams.locationType"
          placeholder="请选择场地类型"
          clearable
          class="!w-240px"
        >
          <el-option label="机房" value="机房" />
          <el-option label="实训室" value="实训室" />
          <el-option label="教室" value="教室" />
          <el-option label="会议室" value="会议室" />
          <el-option label="实验室" value="实验室" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option label="禁用" :value="0" />
          <el-option label="启用" :value="1" />
          <el-option label="维修中" :value="2" />
          <el-option label="预约中" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['ao:class-location:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['ao:class-location:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="warning"
          plain
          @click="handleImport"
        >
          <Icon icon="ep:upload" class="mr-5px" /> 导入
        </el-button>
        <!-- 删除功能已移除，防止误删重要的上课场地基础数据 -->
        <!-- <el-button
            type="danger"
            plain
            :disabled="isEmpty(checkedIds)"
            @click="handleDeleteBatch"
            v-hasPermi="['ao:class-location:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button> -->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
    >
    <el-table-column type="selection" width="55" />
      <el-table-column label="场地ID" align="center" prop="locationId" width="80" />
      <el-table-column label="场地名称" align="center" prop="locationName" min-width="120" />
      <el-table-column label="建筑物ID" align="center" prop="buildingId" width="90" />
      <el-table-column label="楼层" align="center" prop="floor" width="70" />
      <el-table-column label="房间号" align="center" prop="roomNumber" width="90" />
      <el-table-column label="场地类型" align="center" prop="locationType" width="90" />
      <el-table-column label="容量" align="center" prop="classroomCapacity" width="70" />
      <el-table-column label="管理员ID" align="center" prop="managerId" width="90" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 0" type="danger">禁用</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="success">启用</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="warning">维修中</el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="info">预约中</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="设备清单" align="center" prop="equipmentList" show-overflow-tooltip min-width="120" />
      <el-table-column label="预约规则" align="center" prop="bookingRules" show-overflow-tooltip min-width="120" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.locationId)"
            v-hasPermi="['ao:class-location:update']"
          >
            编辑
          </el-button>
          <!-- 删除功能已移除，防止误删重要的上课场地基础数据 -->
          <!-- <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['ao:class-location:delete']"
          >
            删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ClassLocationForm ref="formRef" @success="getList" />
  <!-- 表单弹窗：导入 -->
  <ClassLocationImportForm ref="importFormRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ClassLocationApi, ClassLocation } from '@/api/ao/classlocation'
import ClassLocationForm from './ClassLocationForm.vue'
import ClassLocationImportForm from './ClassLocationImportForm.vue'

/** 上课场地信息 列表 */
defineOptions({ name: 'ClassLocation' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ClassLocation[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  locationName: undefined,
  buildingId: undefined,
  floor: undefined,
  roomNumber: undefined,
  locationType: undefined,
  managerId: undefined,
  status: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ClassLocationApi.getClassLocationPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 导入表单相关 */
const importFormRef = ref()
const handleImport = () => {
  importFormRef.value.open()
}

// 删除功能已移除，防止误删重要的上课场地基础数据
// /** 删除按钮操作 */
// const handleDelete = async (id: number) => {
//   try {
//     // 删除的二次确认
//     await message.delConfirm()
//     // 发起删除
//     await ClassLocationApi.deleteClassLocation(id)
//     message.success(t('common.delSuccess'))
//     // 刷新列表
//     await getList()
//   } catch {}
// }

// /** 批量删除上课场地信息 */
// const handleDeleteBatch = async () => {
//   try {
//     // 删除的二次确认
//     await message.delConfirm()
//     await ClassLocationApi.deleteClassLocationList(checkedIds.value);
//     message.success(t('common.delSuccess'))
//     await getList();
//   } catch {}
// }

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: ClassLocation[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ClassLocationApi.exportClassLocation(queryParams)
    download.excel(data, '上课场地信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
